<?php
class WeixinCodeModel extends PdoModel
{
    protected $_name = 'weixin_code';

    function __construct()
    {
        parent::__construct();
    }

    #添加
    function addData($data){
        if(empty($data)){
            return false;
        }
        return $this->insert($data);
    }

    #更新
    function updateData($data = array(), $uid = 0){
        if(empty($data) || $uid <= 0){
            return false;
        }

        return $this->update($data, "uid = {$uid}");
    }


    function getListData($page = 1,$page_size =  20,$condition = array()){
        $sql = " select * from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid={$condition['uid']} ";
        }
        if(!empty($condition['code'])) {
            $sql .= " and code={$condition['code']} ";
        }
        if(!empty($condition['weixin_openid'])) {
            $sql .= " and weixin_openid='{$condition["weixin_openid"]}'";
        }
        if(!empty($condition['start_date'])) {
            $sql .= " and created_at >={$condition['start_date']} ";
        }
         if(!empty($condition['end_date'])) {
            $sql .= " and created_at <={$condition['end_date']} ";
        }
        $sql .= " order by id desc ";

        $start = ($page -1 ) * $page_size;
        $sql .= " limit {$start}, {$page_size}";
        try{
            $data = $this->fetchAll($sql);
        }catch(Exception $ex){
            $data = array();
        }
        return $data;
    }

    function getListCount($condition = array()){
        $sql = " select count(*) as num from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid={$condition['uid']} ";
        }
        if(!empty($condition['code'])) {
            $sql .= " and code={$condition['code']} ";
        }
        if(!empty($condition['weixin_openid'])) {
            $sql .= " and weixin_openid='{$condition["weixin_openid"]}'";
        }
        if(!empty($condition['start_date'])) {
            $sql .= " and created_at >={$condition['start_date']} ";
        }
        if(!empty($condition['end_date'])) {
            $sql .= " and created_at <={$condition['end_date']} ";
        }

        $result = $this->fetchRow($sql);
        $num = 0;
        if(!empty($result['num'])) {
            $num = $result['num'];
        }
        return $num;
    }
}